<template>
    <div class="image-list" :editable="editable" :class="{selected: selected.length > 0}">
        <ul ref="imagewarp">
            <li v-for="(item,index) in list" :key="item.id || index" :class="{[item.type]:true,selected:selected.indexOf(item.id) >= 0,disabled: isDisabled(item.width,item.height)}" v-loading="item.loading">

                <!-- 上传失败 -->
                <figure v-if="item.type == 'error'">
                    <img v-if="item.image" crossOrigin="anonymous" :src="item.image.src" :style="$fiexImage(item.image.width, item.image.height, 1)" />
                    <strong v-text="'Error: ' + item.error"></strong>
                    <button @click="removeErrorItem(item)">Delete</button>
                </figure>

                <!-- 上传中的 -->
                <figure v-else-if="item.type == 'uploading'">
                    <img v-if="item.image" crossOrigin="anonymous" :src="item.image.src" :style="$fiexImage(item.image.width, item.image.height, 1)" />
                    <UxProgress :rate="item.percent" />
                </figure>

                <!-- 正常显示 -->
                <figure v-else>
                    <img crossOrigin="anonymous" v-lazy="getLitimg(item.img_url,'medium')" :style="$fiexImage(item.width, item.height, 1)" />
                    <i><svg-icon name="checkbox-active" /></i>
                    <b @click="setSelect($event, item)"></b>
                    <span @click="doEditor(item)"><svg-icon name="edit" /></span>
                </figure>

                <div class="image-list-unedited" v-if="item.color_unedited == 0">Default thread color</div>
                <strong v-text="item.file_name"></strong>
                <span v-text="getIn(item.width) + '″×' + getIn(item.height) + '″'"></span>
                <span v-text="item.stitches_count + ' stitches'"></span>
            </li>
        </ul>

    </div>
</template>
<script>

export default {
    props: {
        list: { type: Array, default: () => [] },
        platen: { type: Object, default: () => null },
        selected: { type: Array, default: () => [] },
        editable: { type: Boolean, default: false },
    },
    data () {
        return {
            loading: false,
            cache_name: '',
            preview: null,
        };
    },

    watch: {
        list () {
            const upload = this.list.find(i => i.type == 'uploading')
            upload && setTimeout(() => window && window.scrollTo(0, this.$el.offsetTop), 500);
        }
    },

    methods: {
        // 编辑名字
        async updateName (event, item) {
            let value = item.img_name.trim();
            if (item.loading == true) {
                // 正在加载中的不管
            } else if (item.cache_name == value) {
                // 值相同的不管
            } else if (!/[\w\W]+/.test(value)) {
                item.img_name = item.cache_name;
                this.$uxMessage.error("At least one character");
            } else {
                this.$set(item, "loading", true);
                let response = await this.$http("Material.putMaterialFile/" + item.id, {
                    img_name: value,
                });

                if (response.code === 0) {
                    item.loading = false;
                    item.cache_name = '';
                    this.$uxMessage.success("Image renamed successfully");
                    if (this.$parent.recently && this.$parent.recently.map) {
                        this.$parent.recently.map(i => i.id == item.id && (i.img_name = value))
                    }

                } else {
                    item.img_name = item.cache_name;
                    item.loading = false;
                    this.$uxMessage.error("Failed to rename image");
                }
            }
        },

        // 删除有问题的图片
        removeErrorItem (item) {
            const index = this.$parent.uploaded.indexOf(item);
            index >= 0 && this.$parent.uploaded.splice(index, 1);
        },
        getIn (val) {
            return (val * 0.1 * 0.0394).toFixed(1)
        },

        // 是否不能选中
        isDisabled (width, height) {
            if (!this.platen) {
                return false;
            } else {
                // 比较最长边
                const size = {
                    width: this.platen.width * 254 / 300,
                    height: this.platen.height * 254 / 300,
                }
                const max = Math.max(size.width, size.height) > Math.max(width, height)
                const min = Math.min(size.width, size.height) > Math.min(width, height)
                return !(max && min)
            }
        },


        getLitimg (url, type) {
            return this.$getImageUrl(url)
        },

        // 选中
        setSelect (event, item) {

            let index = this.selected.indexOf(item.id);
            event.stopPropagation();
            event.preventDefault();

            if (index < 0) {
                this.$emit('update:selected', [...this.selected, item.id]);
            } else {
                this.$emit('update:selected', this.selected.filter(i => i != item.id));
            }
        },

        // 预览
        doEditor (item) {

            if (this.$listeners.select) {
                this.$emit("select", item);
            } else {
                this.$emit('edit', item)
            }
        },
    },
    created () { },
    mounted () { },
};
</script>
<style>
.image-preview {
    padding: 50px;
}
.image-preview__image {
    max-width: 100%;
    max-height: 100%;
    background: #fff;
}
.image-preview__action-bar {
    display: none !important;
    border-radius: 20px;
    overflow: hidden;
}
.image-preview__action-rotate-left {
    display: none !important;
}
.image-preview__action-rotate-right {
    display: none !important;
}
.image-preview__horizontal-toggle {
    display: none !important;
}
</style>
<style lang="scss" scoped>
.image-list {
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e6e6e6;

    ul {
        display: flex;
        flex-wrap: wrap;
        margin: 4px 12px;
    }

    li {
        width: calc(16.66% - 16px);
        display: flex;
        flex-direction: column;
        margin: 16px 8px;
        box-sizing: border-box;
        position: relative;

        figure {
            position: relative;
            overflow: hidden;
            background: #f8f8f8;
            border-radius: 4px;
            border: 1px solid #f0f0f0;
            transition: all 0.2s;
            &::before {
                content: '';
                display: block;
                padding-bottom: 100%;
            }
            img {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                pointer-events: none;
            }

            span {
                position: absolute;
                right: 7px;
                bottom: 7px;
                width: 30px;
                height: 30px;
                opacity: 0;
                transition: all 0.2s;
                color: #f6f6f6;
                cursor: pointer;
                background: #000000;
                border-radius: 6px;
                display: flex;
                align-items: center;
                justify-content: center;

                svg {
                    width: 18px;
                    height: 18px;
                }

                &:hover {
                    color: #929292;
                }
            }

            & > b {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
            }

            .ux-progress {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;

                background: rgba($color: #000000, $alpha: 0.3);
                &::v-deep {
                    svg {
                        position: absolute;
                        left: 50%;
                        top: 50%;
                        width: 100px;
                        height: 100px;
                        transform: translate(-50%, -50%);
                    }
                }
            }

            & > i {
                position: absolute;
                left: 8px;
                top: 8px;
                color: #666;
                width: 16px;
                height: 16px;
                background: rgba(255, 255, 255, 1);
                border-radius: 2px;
                border: 1px solid #9e9e9e;
                cursor: pointer;
                pointer-events: none;
                opacity: 0;
                transition: all 0.2s;

                svg {
                    position: absolute;
                    width: 16px;
                    height: 16px;
                    left: 50%;
                    top: 50%;
                    padding: 3px;
                    box-sizing: border-box;
                    transform: translate(-50%, -50%);
                    opacity: 0;
                }

                &:hover {
                    display: block;
                }
            }

            &:hover {
                box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.12);
                & > span {
                    opacity: 1;
                }
                & > i {
                    svg {
                        opacity: 0.3;
                    }
                }
            }
        }

        .input-inline::v-deep {
            position: relative;
            justify-content: flex-start;
            margin-top: 10px;
            .input-inline-icon {
                transition: all 0.2s;
                opacity: 0;
                pointer-events: none;
            }

            &:hover .input-inline-icon {
                pointer-events: all;
                opacity: 1;
            }
        }

        .image-list-unedited {
            display: inline-block;
            margin: 10px auto 0 0;
            padding: 0 6px;
            height: 22px;
            box-sizing: border-box;
            line-height: 20px;
            background: #ffffff;
            border-radius: 4px;
            border: 1px solid #c62828;
            font-family: Roboto, Roboto;
            font-weight: 400;
            font-size: 12px;
            color: #c62828;
            white-space: nowrap;
            overflow: hidden;
        }
        & > strong {
            margin-top: 10px;
            font-family: Roboto, Roboto;
            font-weight: 400;
            font-size: 14px;
            color: #000000;
            line-height: 18px;
        }
        & > span {
            margin-top: 6px;
            font-family: Roboto, Roboto;
            font-weight: 400;
            font-size: 14px;
            color: #666666;
            line-height: 16px;
        }

        &.disabled {
            figure::after {
                content: 'Too large for the frame';
                position: absolute;
                left: 0;
                top: 0;
                right: 0;
                bottom: 0;
                display: flex;
                align-items: center;
                justify-content: center;
                background: rgba(78, 76, 76, 0.8);
                border-radius: 4px;
                font-family: Roboto, Roboto;
                font-weight: 500;
                font-size: 16px;
                color: #ffffff;
                line-height: 22px;
                color: #ffffff;
                text-align: center;
            }
        }

        &.selected figure i {
            &::before {
                background: #ea4c89;
                border-radius: 2px;
                border-color: transparent;
            }
            svg {
                opacity: 1;
                fill: #ea4c89;
            }
        }

        &.error figure {
            z-index: 0;
            &::after {
                content: '';
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                background: rgba($color: #c01317, $alpha: 0.8);
                z-index: 1;
                border-radius: 4px;
            }
            strong {
                position: absolute;
                left: 0;
                top: 0;
                bottom: 44px;
                right: 0;
                padding: 16px 10px;
                font-size: 14px;
                font-family: Roboto-Regular, Roboto;
                font-weight: 400;
                color: #ffffff;
                line-height: 18px;
                z-index: 2;
            }
            button {
                position: absolute;
                max-width: 90px;
                width: calc(100% - 10px);
                height: 28px;
                border: none;
                border-radius: 6px;
                left: 50%;
                transform: translateX(-50%);
                bottom: 16px;
                z-index: 2;
                background: #fff;
                font-size: 14px;
                font-family: Roboto-Regular, Roboto;
                font-weight: 400;
            }
        }
    }

    &[editable] {
        figure {
            cursor: pointer;
            &:hover > i {
                opacity: 1;
                pointer-events: all;
            }
        }
    }

    &.selected {
        figure > i {
            opacity: 1;
            pointer-events: all;
            background-color: #fff;
            border-color: #9e9e9e;
        }
    }
}
</style>
